package tree;

import java.util.HashMap;
import java.util.Map;

public class _106_从中序与后序遍历序列构造二叉树 {
    int post_idx;
    int[] postorder;
    int[] inorder;
    HashMap<Integer, Integer> idx_map = new HashMap<Integer, Integer>();

    public TreeNode buildTree2(int[] inorder, int[] postorder) {
        if (inorder.length == 0 || inorder.length != postorder.length) {
            return null;
        }
        for (int i = 0; i < inorder.length; i++) {
            idx_map.put(inorder[i], i);
        }
        post_idx = postorder.length -1;
        return dfs(0, inorder.length-1, postorder);
    }

    private TreeNode dfs(int start, int end, int[] postorder) {
        if (start>end) {
            return null;
        }
        int rootVal = postorder[post_idx--];
        Integer rootIdx = idx_map.get(rootVal);
        TreeNode root = new TreeNode(rootVal);
        root.right = dfs(rootIdx+1, end, postorder);
        root.left = dfs(start, rootIdx -1, postorder);
        return root;
    }


    public TreeNode helper(int in_left, int in_right) {
        // if there is no elements to construct subtrees
        if (in_left > in_right)
            return null;

        // pick up post_idx element as a root
        int root_val = postorder[post_idx];
        TreeNode root = new TreeNode(root_val);

        // root splits inorder list
        // into left and right subtrees
        int index = idx_map.get(root_val);

        // recursion
        post_idx--;
        // build right subtree
        root.right = helper(index + 1, in_right);
        // build left subtree
        root.left = helper(in_left, index - 1);
        return root;
    }


    /**
     * 中序遍历 inorder = [9,3,15,20,7]
     * 后序遍历 postorder = [9,15,7,20,3]
     *
     * @param inorder
     * @param postorder
     * @return
     */
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        this.postorder = postorder;
        this.inorder = inorder;
        // start from the last postorder element
        post_idx = postorder.length - 1;

        // build a hashmap value -> its index
        int idx = 0;
        for (Integer val : inorder)
            idx_map.put(val, idx++);
        return helper(0, inorder.length - 1);
    }


    static int postIdx;

    public static TreeNode buildTree1(int[] inorder, int[] postorder) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }
        postIdx = postorder.length - 1;
        return doBuild(0, inorder.length - 1, postorder, map);
    }

    private static TreeNode doBuild(int start, int end, int[] postorder, Map<Integer, Integer> map) {
        if (start > end) {
            return null;
        }
        int rootVal = postorder[postIdx--];
        Integer rootIdx = map.get(rootVal);
        TreeNode treeNode = new TreeNode(rootVal);
        treeNode.right = doBuild(rootIdx + 1, end, postorder, map);
        treeNode.left = doBuild(start, rootIdx - 1, postorder, map);
        return treeNode;
    }

    public static void main(String[] args) {
        TreeNode treeNode = buildTree1(new int[]{9, 3, 15, 20, 7}, new int[]{9, 15, 7, 20, 3});
    }
}
